<template>
  <div class="main">
    <el-form ref="user" status-icon :model="user" label-width="80px" :rules="rules">
      <el-form-item label="用户名">
        <el-input v-model="user.userId" disabled />
      </el-form-item>
      <el-form-item label="原密码" prop="user_Pwd">
        <el-input v-model="user.user_Pwd" autocomplete="off" disabled />
      </el-form-item>
      <el-form-item label="密码" prop="userPwd">
        <el-input v-model="user.userPwd" autocomplete="off" type="password" />
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <el-input v-model="user.checkPass" type="password" autocomplete="off" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('user')">提交</el-button>
        <el-button @click="resetForm('user')">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- <el-input placeholder="请输入新密码" />
    <el-button type="primary">确定修改</el-button> -->
  </div>
</template>

<script>
import { update } from '../../api/user.js'
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else if (value.length < 5) {
        callback(new Error('请输入五位以上的密码！'))
      } else if (value.length > 15) {
        callback(new Error('请输入十五位以下的密码！'))
      } else {
        callback()
      }
    }
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.user.userPwd) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      rules: {
        userPwd: [{
          validator: validatePass, trigger: 'blur'
        }],
        checkPass: [
          { validator: validatePass2, trigger: 'blur' }
        ]
      },
      user: {
        userId: '',
        userPwd: '',
        userType: '',
        checkPass: '',
        user_Pwd: ''
      }
    }
  },
  created() {
    this.get_loc_data()
  },
  updated() {
    this.get_loc_data()
  },
  methods: {
    get_loc_data() {
      var user_Id = localStorage.getItem('user_Id')
      var user_Pwd = localStorage.getItem('user_Pwd')
      var user_Type = localStorage.getItem('user_Type')
      this._data.user.userId = user_Id
      this._data.user.user_Pwd = user_Pwd
      this._data.user.userType = user_Type
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$confirm('是否确认修改？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            update(this._data.user).then((res) => {
              this.$message({
                message: '修改成功',
                type: 'success'
              })
              localStorage.setItem('user_Pwd', this._data.user.userPwd)
              this.$refs[formName].resetFields()
            })
          }).catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            })
          })
          //   var new_pwd = this._data.user.userPwd
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style>
    .main{
        width: 600px;
        /* height: 400px; */
        margin-top: 20px;
    }
</style>
